package com.csust.automaticteach.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.constant.OrderConstant;
import com.csust.automaticteach.model.dto.errorpractice.ErrorPracticeQueryRequest;
import com.csust.automaticteach.model.entity.ErrorPractice;
import com.csust.automaticteach.model.vo.errorpratice.ErrorPracticeVO;
import com.csust.automaticteach.service.ErrorPracticeService;
import com.csust.automaticteach.mapper.ErrorPracticeMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 钰玟
* @description 针对表【t_error_practice(错题表)】的数据库操作Service实现
* @createDate 2025-07-12 14:34:17
*/
@Service
public class ErrorPracticeServiceImpl extends ServiceImpl<ErrorPracticeMapper, ErrorPractice>
    implements ErrorPracticeService{

    @Override
    public QueryWrapper<ErrorPractice> getErrorPracticeQueryWrapper(ErrorPracticeQueryRequest errorPracticeQueryRequest) {
        String title = errorPracticeQueryRequest.getTitle();
        String type = errorPracticeQueryRequest.getType();
        String sortField = errorPracticeQueryRequest.getSortField();
        String sortOrder = errorPracticeQueryRequest.getSortOrder();

        QueryWrapper<ErrorPractice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.eq(StrUtil.isNotBlank(type), "type", type);
        queryWrapper.orderBy(StrUtil.isNotBlank(sortField), OrderConstant.ASC.equals(sortOrder), sortField);
        return queryWrapper;
    }

    @Override
    public ErrorPracticeVO getErrorPracticeVO(ErrorPractice errorPractice) {
        ErrorPracticeVO errorPracticeVO = new ErrorPracticeVO();
        String optionsJson = errorPractice.getOptions();
        BeanUtil.copyProperties(errorPractice, errorPracticeVO);
        List<String> options = JSONUtil.toList(optionsJson, String.class);
        List<String> knowledgePoints = JSONUtil.toList(errorPractice.getKnowledgePoints(), String.class);
        errorPracticeVO.setOptions(options);
        errorPracticeVO.setKnowledgePoints(knowledgePoints);
        return errorPracticeVO;
    }

    @Override
    public List<ErrorPracticeVO> getErrorPracticeVOList(List<ErrorPractice> records) {
        return records.stream().map(this::getErrorPracticeVO).toList();
    }
}




